import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydau -o ydau.json
https://www.69t285.com/
'''


class YdauSpider(scrapy.Spider):
    name = "ydau"
    # 前缀
    prefix = 'https://www.'
    # 中缀
    website = '69t285'
    # 后缀
    suffix = '.com/'
    allowed_domains = [website + '.com']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        video_url = list(set(video_url))
        names = response.xpath("//meta[@property='og:title']/@ content").extract()
        p_url = response.xpath("//meta[@property='og:image']/@ content").extract()
        tags = response.xpath("//div[@id='tab_video_info']//div//div//div[@class='item']//a/text()").extract()
        if len(video_url) and len(names) and len(p_url) and len(tags):
            for i in range(len(video_url)):
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=tags[0], url="", name=  re.sub(r'【.*?】', '', names[0]).strip(), pUrl=p_url[0], vUrl=video_url[i])
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('https') and url.endswith('/'):
                if not url.endswith('.jpg/'):
                    yield scrapy.Request(url, callback=self.parse)
